package com.ruoyi.web.controller.excelHelper;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.web.domain.FiltVo;
import com.ruoyi.framework.web.domain.excel.FileInfo;
import com.ruoyi.web.service.IExcelService;
import com.ruoyi.web.service.IFileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelHandleController extends BaseController {
    @Value("${excel.directoryOfFilted}")
    private String directoryOfFilted;

    @Autowired
    private IFileInfoService FileInfoService;

    /**
     * 查询文件信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FileInfo FileInfo)
    {
        List<FileInfo> list = FileInfoService.selectFileInfoList(FileInfo);
        return getDataTable(list);
    }
    @Autowired
    private IExcelService iExcelService;
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MultipartFile file, String s){
        return iExcelService.add(file);
    }

    @PostMapping("/filt")
    public void filt(@RequestBody FiltVo filtVo){
        log.debug("接收到文件id：{},\n接收到查询条件：{} ",filtVo.getFileIds(), filtVo.getNumbers());
        iExcelService.filt(filtVo);
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/{fileId}")
    public AjaxResult remove(@PathVariable String[] fileId)
    {
       return iExcelService.remove(fileId);
    }

    /**
     * 压缩包下载
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
            byte[] data = iExcelService.download();
            genCode(response,data);
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"filted.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
